package com.bye.xiaomi_mall.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.bye.xiaomi_mall.domain.*;
import com.bye.xiaomi_mall.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bye.xiaomi_mall.service.ITdUserService;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class TdUserServiceImpl implements ITdUserService {
    @Autowired
    private TdUserMapper tdUserMapper;
    @Autowired
    private TdShoppingCartMapper shoppingCartMapper;
    @Autowired
    private TdGoodsMapper goodsMapper;
    @Autowired
    private TdGoodsSpecificationsMapper specificationsMapper;
@Autowired
private TdAddressMapper addressMapper;

    /**
     * 查询用户
     *
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public TdUser selectTdUserById(Long id) {
        return tdUserMapper.selectTdUserById(id);
    }

    /**
     * 查询用户列表
     *
     * @param tdUser 用户
     * @return 用户
     */
    @Override
    public List<TdUser> selectTdUserList(TdUser tdUser) {
        return tdUserMapper.selectTdUserList(tdUser);
    }

    /**
     * 新增用户
     *
     * @param tdUser 用户
     * @return 结果
     */
    @Override
    public int insertTdUser(TdUser tdUser) {
        tdUser.setCreateTime(DateUtils.getNowDate());
        return tdUserMapper.insertTdUser(tdUser);
    }

    /**
     * 修改用户
     *
     * @param tdUser 用户
     * @return 结果
     */
    @Override
    public int updateTdUser(TdUser tdUser) {
        return tdUserMapper.updateTdUser(tdUser);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteTdUserByIds(Long[] ids) {
        return tdUserMapper.deleteTdUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteTdUserById(Long id) {
        return tdUserMapper.deleteTdUserById(id);
    }

    @Override
    public TdUser selectTdUserByPhone(String phone) {
        return tdUserMapper.selectTdUserByPhone(phone);
    }

    @Override
    public List<TdShoppingCart> selectShoppingCart(String userId) {
        List<TdShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(userId);
        for (TdShoppingCart cart : shoppingCartList) {
            TdGoods goods = goodsMapper.selectTdGoodsById(cart.getGoodsId());
            cart.setGood(goods);
            List<String> spNameList = new ArrayList<>();
            List<String> spValueList = new ArrayList<>();
            String goodsSp = cart.getGoodsSp();
            String[] strings = goodsSp.split("[,，]");
            for (String s : strings) {
                String[] s1 = s.split("_");
                if (s1.length >= 2) {
                    spNameList.add(s1[0]); // 规格名
                    spValueList.add(s1[1]); // 规格值
                } else {
                    System.out.println("无效的规格格式: " + s);
                }
            }
            cart.setSpValueList(spValueList);
            cart.setSpNameList(spNameList);
            List<String> spAddMoneyList = selectSpAddMoney(spNameList, spValueList);
            cart.setSpMoneyList(spAddMoneyList);
        }
        return shoppingCartList;
    }

    @Override
    public List<TdAddress> selectAddress(String userId) {
        return addressMapper.selectAddress(userId);
    }

    private List<String> selectSpAddMoney(List<String> spNameList, List<String> spValueList) {
        List<String> spAddMoneyList = new ArrayList<>();
        for (int i = 0; i < spNameList.size(); i++) {
            TdGoodsSpecifications sp = specificationsMapper.selectSpByName(spNameList.get(i));
            List<String> value = Arrays.asList(sp.getValue().split("[,，]"));
            List<String> money = Arrays.asList(sp.getAddMoney().split("[,，]"));
            int index = value.indexOf(spValueList.get(i));
            if (index != -1) {
                spAddMoneyList.add(money.get(index));
            }
        }
        return spAddMoneyList;
    }

}
